package com.ctakit.kiwi.common.core.bean.selectors;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.util.*;

/**
 * @author lizhenmin
 */
@Slf4j
public class CompositeSelector extends KeyValueMapSelector {
    private static final List<KeyValueMapSelector> SINGLETON_NULL_LIST = Collections.singletonList(null);
    private final List<KeyValueMapSelector> selectors;

    public CompositeSelector(KeyValueMapSelector... selectors) {
        if (ArrayUtils.isEmpty(selectors)) {
            this.selectors = new LinkedList<KeyValueMapSelector>();
        } else {
            this.selectors = new LinkedList<KeyValueMapSelector>(Arrays.asList(selectors));
            removeNullSubSelectors();
        }
    }

    CompositeSelector() {
        this.selectors = new LinkedList<KeyValueMapSelector>();
    }

    CompositeSelector(List<? extends KeyValueMapSelector> selectors) {
        this.selectors = new ArrayList<KeyValueMapSelector>(ListUtils.emptyIfNull(selectors));
        removeNullSubSelectors();
    }

    private void removeNullSubSelectors() {
        this.selectors.removeAll(SINGLETON_NULL_LIST);
    }

    CompositeSelector addSubSelector(KeyValueMapSelector subSelector) {
        if (null != subSelector) {
            this.selectors.add(subSelector);
        }
        return this;
    }

    @Override
    protected Map<String, Object> doSelect(Object item) {
        if (CollectionUtils.isEmpty(selectors)) {
            log.debug("Not any subSelectors found, returning emptyMap");
            return Collections.emptyMap();
        }

        Map<String, Object> result = new LinkedHashMap<String, Object>();
        for (KeyValueMapSelector selector : selectors) {
            result.putAll(selector.select(item));
        }
        return result;
    }
}
